package com.gc.controller;

import com.gc.common.constants.FileConstants;
import com.gc.common.resp.ApiResult;
import com.gc.common.util.MyUtil;
import com.gc.dto.VehicleImageDTO;
import com.gc.pojo.SysMhyMedia;
import com.gc.pojo.SysVehicleInfo;
import com.gc.services.MhyService;
import com.gc.services.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("vehicle")
public class VehicleController {


    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private MhyService mhyService;


    /**
     * 上传 基本信息实体类
     *
     * @param info
     * @return
     */
    @PostMapping("add")
    public ApiResult add(@RequestBody SysVehicleInfo info) {
        int count = vehicleService.addVehicleInfo(info);
        if (count == 1) {
            return new ApiResult();
        } else {
            return new ApiResult(500, "添加失败!");
        }
    }

    /**
     * 上传检测过程照片
     * @param vi
     * @return
     */
    @PostMapping("/image/upload")
    public ApiResult imageUpload(@RequestBody @Validated VehicleImageDTO vi) {
        int count = 0;
        SysMhyMedia media = mhyService.getMhyMediaByUUId(vi.getUuid());
        vi.setFileData(MyUtil.Base64ToImage(vi.getFileData(), FileConstants.VOR_IMG_LOCAL_URL));
        if (ObjectUtils.isEmpty(media)) {
            count = mhyService.addMhyMedia(vi);
        } else {
            count = mhyService.modifyMhyMedia(vi, media.getMediaId());
        }
        if (count == 1) {
            return new ApiResult();
        } else {
            return new ApiResult(500, "添加失败!");
        }
    }

}
